package com.hzdl.car.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzdl.car.bean.Menu;
import com.hzdl.car.bean.Role;
import com.hzdl.car.mapper.MenuMapper;
import com.hzdl.car.mapper.RoleMapper;
import com.hzdl.car.service.RoleService;
import com.hzdl.car.util.Common;
import com.hzdl.car.util.TreeNode;
import com.hzdl.car.vo.MenuVo;
import com.hzdl.car.vo.RoleVo;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private MenuMapper menuMapper;

    @Override
    public PageInfo<Role> findRole(RoleVo roleVo) {
        PageHelper.startPage(roleVo.getPage(), roleVo.getLimit());
        List<Role> list = roleMapper.findRole(roleVo);
        PageInfo<Role> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public int addRole(RoleVo roleVo) {
        return roleMapper.addRole(roleVo);
    }

    @Override
    public int update(RoleVo roleVo) {
        return roleMapper.update(roleVo);
    }

    @Transactional
    @Override
    public int delRole(RoleVo roleVo) {
        int row = 0;
        row += roleMapper.delRole(roleVo);
        row += roleMapper.delRoleMenu(roleVo);
        row += roleMapper.delRoleUser(roleVo);
        return row;
    }

    @Override
    public int chkRoleUserMenu(RoleVo roleVo) {
        int row = 0;
        row += roleMapper.chkRoleMenu(roleVo);
        row += roleMapper.chkRoleUser(roleVo);
        return row;
    }

    @Transactional
    @Override
    public int roleDelChk(Integer[] roleIds) {
        int row = 0;
        for (Integer i : roleIds) {
            RoleVo roleVo = new RoleVo();
            roleVo.setRoleId(i);
            row += roleMapper.delRole(roleVo);
        }
        return row;
    }

    @Override
    public List<TreeNode> findMenuByRole(RoleVo roleVo) {
        //获取所有菜单
        MenuVo menuVo = new MenuVo();
        menuVo.setAvailable(1);
        List<Menu> menus = menuMapper.findAllMenu(menuVo);


        List<Menu> roleMenu = menuMapper.findMenuByRole(roleVo);
        List<TreeNode> treeNodes = new ArrayList<>();
        for (
                Menu m1 : menus) {
            String checkArr = "0";
            for (Menu m2 : roleMenu) {
                if (m1.getId() == m2.getId()) {
                    checkArr = "1";
                    break;
                }
            }
            // List<Menu> 转换为 List<TreeNode>
            treeNodes.add(new TreeNode(m1.getId(), m1.getpId(), m1.getName(), m1.getOpen() == 1 ? true : false, checkArr));
        }

        // 转换为有乘机关系的样子
        List<TreeNode> treeNodesAfter = new ArrayList<>();
        Common.getTreeNodeMenu(treeNodes, 0, treeNodesAfter);
        return treeNodesAfter;
    }

    @Override
    public void saveRoleMenu(RoleVo roleVo) {
        // 1. 删除sys_role_menu
        roleMapper.delMenuByRole(roleVo);
        // 2. 插入sys_role_menu
        for (Integer id : roleVo.getIds()){
            roleMapper.addRoleMenuByRole(roleVo.getRoleId() , id);
        }

    }


}
